package com.lehuowujie.lehuomarket.photo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.lehuowujie.lehuomarket.R;
import com.lehuowujie.lehuomarket.photo.imageloader.ImageLoader;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 王瑞铭 on 2017/1/12.
 */
public class ImageAdapter extends BaseAdapter {

    private static Set<String> mSelectImgs = new HashSet<>();

    private String mDirPath;
    private List<String> mImgPaths;
    private LayoutInflater mInfater;

    public ImageAdapter(Context context, List<String> mDatas, String dirPath) {
        this.mDirPath = dirPath;
        mImgPaths = mDatas;
        mInfater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mImgPaths.size();
    }

    @Override
    public Object getItem(int i) {
        return mImgPaths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            view = mInfater.inflate(R.layout.item_gridview, viewGroup, false);
            holder = new ViewHolder();
            holder.mImg = (ImageView) view.findViewById(R.id.item_img);
            holder.mSelect = (ImageButton) view.findViewById(R.id.item_select);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //先设置成默认图片,未选中状态
        holder.mImg.setImageResource(R.mipmap.pictures_no);
        holder.mSelect.setImageResource(R.mipmap.picture_unselected);
        holder.mImg.setColorFilter(null);
        ImageLoader.getInstance(3, ImageLoader.Type.FIF0).loadImage(mDirPath + "/" + mImgPaths.get(i), holder.mImg);
        final String filePath = mDirPath + "/" + mImgPaths.get(i);
        if (mSelectImgs.contains(filePath))
        {
            holder.mImg.setColorFilter(Color.parseColor("#77000000"));
            holder.mSelect.setImageResource(R.mipmap.pictures_selected);
        }
        holder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mSelectImgs.contains(filePath))
                {
                    //已经被选中
                    mSelectImgs.remove(filePath);
                    holder.mImg.setColorFilter(null);
                    holder.mSelect.setImageResource(R.mipmap.picture_unselected);
                }else
                {
                    mSelectImgs.add(filePath);
                    holder.mImg.setColorFilter(Color.parseColor("#77000000"));
                    holder.mSelect.setImageResource(R.mipmap.pictures_selected);
                }
            }
        });
        return view;
    }

    private class ViewHolder {
        ImageView mImg;
        ImageButton mSelect;
    }
}
